<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Test extends Controller_Index_Base
{
    public function before()
    {
       // die('dsds');
    }
    
    /*public function action_index()
    {
        if (HTTP_Request::POST == $this->request->method()) {
            $email    = $this->request->post('email');
            //$password = $this->request->post('password');
            $url      = $this->request->post('url');
                  
            try {
                $password = ORM::factory('user')->generate_password();
                
                $user = ORM::factory('user')->add_user($email, $password);
                $shop = ORM::factory('shop')->add_shop($url);                                
                $_POST = array();
                 
                $user->add_to_shop($shop); 
                Model_Auth::instance()->login($email, $password, true);
                  
                $shop->set_as_current();                     
                Request::current()->redirect(Helper_Admin::get_url() . '/index/start'); 
            } catch (Model_Exception $e) {
                $message = 'Форма заполнена с ошибками!';
            }
        }
        
        //Model_Auth::instance();
                
        $content = new View('index/index/index_tpl');        
        $this->print_template('', $content); 
    }*/
    
    public function action_test()
    {
        die();
        set_time_limit(0);
        $shop_id = $this->request->param('id') ? $this->request->param('id') : null;
        if (!$shop_id) {
            die();    
        }
        $shop = ORM::factory('shop', $shop_id);
        $shop->set_as_current();
        Model_ProductParser_YML::instance()->parse($shop);
    }
    
    public function action_test1()
    {
        die();
        /*$shop = ORM::factory('shop', 26);
        $shop->set_as_current();
        Model_ProductParser_YML::instance()->parse($shop);*/   
        
        set_time_limit(0);
        $shop = ORM::factory('shop', 27);
        $shop->set_as_current();
        ORM::factory('model')->process_shop($shop);  
        
        /*$model = ORM::factory('model', 27);
        Model_ReviewParser_YandexMarket::instance()->parse($model);*/ 
        
        /*$yes = 132;
        $no = 1111;
        echo round(((($yes + 1.9208) / ($yes + $no) - 
            1.96 * sqrt(($yes * $no) / ($yes + $no) + 0.9604) / 
            ($yes + $no)) / (1 + 3.8416 / ($yes + $no))) * 10000); */
            
        //echo Model_Auth::instance()->hash_password('123456');   
    }
    
    public function action_test2()
    {
        die();
        set_time_limit(0);
        $shop = ORM::factory('shop', 27);
        $shop->set_as_current();
        ORM::factory('model')->process_shop2($shop); 
    }
    
	/*public function action_index1()
	{    
        $product = ORM::factory('product')->find(123);        
        

        $category = ORM::factory('category', 3);
        //$category = new Model_Category(1);
        //$category = ORM::factory('category')->find(1);   
        
        //$categories = ORM::factory('category')->get_childs();    
        $categories = $category->get_childs();
        $breadcrumbs = $category->get_breadcrumbs(); 
        
        $products = ORM::factory('product')->get_by_category($category);
        
        
        foreach($breadcrumbs as $category) {
            echo $category->name.' -> ';
        }
        echo '<br />';
        
        foreach($categories as $category) {
            echo $category->name.' - '.$category->get_parent()->id.'<br />';
        }
        
        foreach($products as $product) {
            echo $product->name.'  <img src="'.$product->picture.'" /><br />';   
        }
        
        die();
        		
        //$this->print_template($title, $content, $menu);
	} */
    
    /*public function action_test3() {
        $models = ORM::factory('model')->find_all();
        foreach ($models as $model) {
            $reviews = ORM::factory('review')->get_reviews($model);
            foreach ($reviews as $review) {
                $review->usefulness = $review->get_usefulness();
                $review->save();
                //$model->change_count_stars($review->rating);
            }
        }
    }     */
    
    public function action_test3()
    {
        die();
        //echo Model_Auth::instance()->hash_password('123456');
        
        //ORM::factory('user')->add_user('akazorin@gmail.com', 'abc26101991xyz'); 
        
        /*$user = ORM::factory('user', 1);
        $shop = ORM::factory('shop', 3);
        $user->add_to_shop($shop);*/
        
        //var_dump(Model_Auth::instance()->login('akazorin@gmail.com', 'abc26101991xyz', true));
        //Model_Auth::instance()->logout();
        //Model_Auth::instance()->force_login('akazorin@gmail.com');
        //var_dump(Model_Auth::instance()->get_user()->in_shop(ORM::factory('shop', 3))); 
        //var_dump(Model_Auth::instance()->auto_login());
        //var_dump(Model_Auth::instance()->check_password('abc26101991xyz'));
        //var_dump(Model_Auth::instance()->logged_in(Model_User_Shop::ROLE_ADMINISTRATOR));
        
        /*DB::query(NULL, 'CREATE TABLE `ugol`.`categories_2` (
                            `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                            `name` VARCHAR( 255 ) NOT NULL ,
                            `parent_id` SMALLINT UNSIGNED NOT NULL DEFAULT \'0\',
                         INDEX ( `parent_id` )
                         ) ENGINE = MYISAM'
        )->execute();   */
        
        
    } 
    
    public function action_test4()
    {
		die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->offset(6700)
            ->limit(500)
            ->find_all(); 
        foreach ($models as $model) {
            echo '#' . $model->id . ' - ' . $model->name . ':<br />';
            $urls = Model_ReviewParser_Rozetka::instance()->get_reviews_urls($model);
            if (count($urls)) {
                $model->set_rozetka_urls($urls);
                echo '<br /><br />';      
            }  
        }  
    }
    
   /* public function action_test5()
    {
        set_time_limit(0);
        //$model = ORM::factory('model', 2699);
        //Model_ReviewParser_Rozetka::instance()->parse($model); 
        $models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_Rozetka::instance()->parse($model);
        }
    }       */
    
    public function action_test6()
    {
		die();
		set_time_limit(0);
        $models = ORM::factory('model')
            ->where('market_url', '!=', NULL)
            ->and_where('market_url', '!=', '#')
            ->offset(6900)
            ->limit(300)
            ->find_all();
        foreach ($models as $model) {
            $id = (int)str_replace('http://market.yandex.ru/model-opinions.xml?modelid=', '', $model->market_url);           
            $url = 'http://market.yandex.ru/model.xml?modelid='.$id;
            
            $source = Model_ReviewParser_Abstract::_get_source_code($url);
            $source = Model_ReviewParser_Abstract::_cut_str('<span class="b-model-pictures__big"', '</span>', $source);
			/*$img = Model_ReviewParser_Abstract::_cut_str(' href="', '"', $source);
			if (!$img) {
				$img = Model_ReviewParser_Abstract::_cut_str(' src="', '"', $source);
			}*/
			$img = Model_ReviewParser_Abstract::_cut_str(' src="', '"', $source);
			if (!$img) {
				continue;
			}
			
			$dir = APPPATH.'../media/uploads/models/';
			$dir = $dir.sprintf('%02x', $model->id % 0xff);
			if (!is_dir($dir)) {
				mkdir($dir);
			}
			$dir = $dir.sprintf('/%d', $model->id);
			if (!is_dir($dir)) {
				mkdir($dir);
			}
			
			$data = file_get_contents($img);
			$f = fopen($dir.'/main.jpg', 'w+');
			fputs($f, $data);
			fclose($f);
			
            echo $model->market_url.' '.$id.' '.$img.'<br />';   
        }
                                               
    }
    
    public function action_test5()
    {
        die();
        set_time_limit(0);
        $tmp_models = ORM::factory('model')
            ->order_by('id', 'DESC')  
            ->offset(60000)
            ->limit(10000)
            ->find_all();
        $models = array();
        foreach ($tmp_models as $model) {
            $models[$model->id] = $model;    
        }
        foreach ($models as &$model) {
            $doubles = ORM::factory('model')
                        ->where('name', 'LIKE', $model->name)
                        ->and_where('id', '!=', $model->id)
                        ->find_all();
            echo $model->id.' - '.$model->name.':<br />';
            foreach ($doubles as $double) {
                echo '<span style="color: red;">' . $double->name . '</span><br />';
                
                /*DB::delete('reviews')
                    ->where('model_id', '=', $double->id)
                    ->execute();  */
                  
                DB::update('products')
                    ->set(array('model_id' => $model->id))
                    ->where('model_id', '=', $double->id)
                    ->execute();    
                    
                unset($models[$double->id]);
                $double->delete();   
            } 
        }
    }   
    
    public function action_test7()
    {
        die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            echo '#' . $model->id . ' - ' . $model->name . ':<br />';
            $urls = Model_ReviewParser_Otzovik::instance()->get_reviews_urls($model);
            if (count($urls)) {
                $model->set_otzovik_urls($urls);
                echo '<br /><br />';      
            }  
        }  
    }
    
    public function action_test8()
    {
        die();
        set_time_limit(0);
        //$model = ORM::factory('model', 2707);
        $model = ORM::factory('model', 580);  
        Model_ReviewParser_Otzovik::instance()->parse($model); 
        /*$models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_Rozetka::instance()->parse($model);
        }  */
    }
    
    // Модели с м
    public function action_test9()
    {  
        die();
        set_time_limit(0);     
        $category_id  = 222;
        $category_url = 'http://market.yandex.ru/guru.xml?CMD=-RR=0,0,0,0-VIS=70-CAT_ID=1620138-EXC=1-PG=10&hid=91610';
		
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('class="b-pager__next" href="', '"', $data)) {
                return 'http://market.yandex.ru'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str('class="b-offers b-offers_type_guru"', 'class="b-model-actioins__shoplist g-none"', $data)) {
                return $_data;
            } 
            return null;
        } 
        
        function get_model_link($data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str('class="b-offers__name" href="', '&amp;', $data)) {
                return 'http://market.yandex.ru'.$link;
            } 
			if ($link = Model_ReviewParser_Abstract::_cut_str('href="/model.xml?', '"', $data)) {
                return 'http://market.yandex.ru/model.xml?'.$link;
            } 
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str('<h3 class="b-offers__title">', '</a>', $data)) {
                return strip_tags($name);
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str('class="b-offers__img" src="', '"', $data)) {
                return $image;
            }
			if ($image = Model_ReviewParser_Abstract::_cut_str('src="http://mdata.yandex.net/i', '"', $data)) {
                return 'http://mdata.yandex.net/i'.$image;
            }			
            return null;   
        }
        
        function get_model_count_prices($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str('Цены</a>', '</li', $data)) {
                if ($count = Model_ReviewParser_Abstract::_cut_str('class="b-model-actioins__cnt">', '</span', $count)) {
                    return (int)$count;
                } 
            } 
            return 0;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str('Отзывы</a>', '</li', $data)) {
                if ($count = Model_ReviewParser_Abstract::_cut_str('class="b-model-actioins__cnt">', '</span', $count)) {
                    return (int)$count;
                } 
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);   
                $count_prices   = get_model_count_prices($model_data);   
                $count_reviews  = get_model_count_reviews($model_data);
				DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_prices', 'count_reviews'))
                    ->values(array($category_id, $name, $link, $image, $count_prices, $count_reviews))->execute();			  
                //echo  $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_prices . ' - ' . $count_reviews . '<br />';
                    
            }
			echo $category_url . '<br />';

			usleep(700000);
            //sleep(1);

            /*$i++;
            if ($i > 5) die();   */
            
        } while ($category_url = get_next_page_url($data));
        
    }
    
    public function action_test10()
    {
        die();
        set_time_limit(0);
        $file = fopen('models123.txt', 'w+');
        $i = 0;
        while (1) {
            $models = DB::select()->from('models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
                fputs($file, $model['id'] . '---' . $model['name'] . "\r\n"); 
            }  
            $i++;
        }
        fclose($file); 
    }
    
    public function action_test11()
    {
        die();
        set_time_limit(0);
        $i = 144;
        while (1) {
            $models = DB::select()->from('tmp_models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
                /*//$name = iconv('utf-8', 'cp1251', $model['name']);
                $name =  $model['goodname'];
                $words = preg_split('/\s/i', $name);
                $name = implode(' ', $words);
                //$name = iconv('cp1251', 'utf-8', $name);  
                echo $name.'<br />';      
                //DB::update('tmp_models')->value('goodname', $name)->where('id', '=', $model['id'])->execute();*/
                
                
                $tmp_name = mb_strtolower(str_replace(array(' ', '-', '(', ')', '.', ',', '+', '/', '&amp;'), '', $model['goodname']));
                //echo $tmp_name.'<br />';
                DB::update('tmp_models')->value('tmp_name', $tmp_name)->where('id', '=', $model['id'])->execute();				
            }  
            $i++;
        }    
    }
    
    public function action_test12()
    {
        die();
        set_time_limit(0); 
        $file = fopen('models123.txt', 'r+');
		$i = 0;
        while ($str = fgets($file)) {
			$i++;
            list($id, $name) = explode('---', $str);
			//if ($id > 154921) {
				//DB::update('tmp_models')->value('goodname', $name)->where('id', '=', $id)->execute();
                DB::update('models')->value('name', trim($name))->where('id', '=', $id)->execute(); 
			//}
        }  
		echo $i;		
    }
	
	public function action_test13()
	{
        die();
		set_time_limit(0); 
        $file = fopen('models_searches.txt', 'r+');
		$i = $j = 0;
        while ($str = fgets($file)) {
			$i++;
            list($name, $searches) = explode("\t", $str);
			$name = str_replace(array(' ', '-', '(', ')', '.', ',', '+', '/', '&amp;'), '', $name);
			$models = DB::select('id', 'tmp_name')->from('tmp_models')->where('tmp_name', 'LIKE', '%'.$name.'%')->and_where('count_searches', 'IS', NULL)->execute()->as_array();
			if (count($models) == 0) {
				$j++;
				continue;
			}
			$model = $models[0];
			DB::update('tmp_models')->value('count_searches', $searches)->where('id', '=', $model['id'])->execute();
        }  
		echo $i . ' - ' . $j;
	}
	
	public function action_test14()
	{
        die();
		set_time_limit(0);
        $i = 144;
        while (1) {
            $models = DB::select()->from('tmp_models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
				$_link = $model['link'].'&';
				if (!($modelid = Model_ReviewParser_Abstract::_cut_str('modelid=', '&', $_link))) {
					die($model['id']);
				}
				$link = 'http://market.yandex.ru/model-opinions.xml?modelid='.$modelid;
                
                DB::update('tmp_models')->value('reviews_link', $link)->where('id', '=', $model['id'])->execute();				
            }  
            $i++;
        }    
	}
	
	public function action_test15()
	{
        die();
		set_time_limit(0);
        $i = 0;
        while (1) {
            $models = DB::select()->from('tmp_models')->where('count_reviews', '>', 0)->or_where('count_searches', '>', 0)->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
				$items = DB::select()->from('models')->where('market_url', '=', $model['reviews_link'])->execute()->as_array();
				if (count($items) == 1) {
					DB::update('models')->value('potential_count_reviews', $model['count_reviews'])
									    ->value('count_searches', $model['count_searches'])->where('id', '=', $items[0]['id'])->execute();	
				} else {
					try { 
						DB::insert('models', array('name', 'potential_count_reviews', 'count_searches', 'market_url'))
							->values(array($model['name'], $model['count_reviews'], $model['count_searches'], $model['reviews_link']))->execute();
					} catch (Database_Exception $e) {
						if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
							continue;
						}
						die($e->getMessage());
					}
				}			
            }  
            $i++;
        }    
	}
	
	public function action_test16()
	{
		die();
        set_time_limit(0);
        $models = ORM::factory('model')
			->where('id', '>', 28001)
			->and_where('id', '<', 61666)
			->and_where('potential_count_reviews', '>', 0)
            ->offset(0)
            ->limit(500)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_YandexMarket::instance()->parse($model);
        }
	}
	
	public function action_test17()
	{
		die();
		$ids = DB::query(Database::SELECT, 'SELECT t1.id FROM yandexmarket_reviews AS t1
							   JOIN yandexmarket_reviews AS t2 ON (t1.model_id = t2.model_id AND t1.source_id = t2.source_id AND t1.id < t2.id)'
        )->execute()->as_array();
		foreach ($ids as $id) {
			DB::delete('yandexmarket_reviews')
                    ->where('id', '=', $id)
                    ->execute();
		}
	}
	
	public function action_test18() {
		die();
		set_time_limit(0);
        $models = ORM::factory('model')
				->offset(60000)
				->limit(5000)
				->find_all();	// where('count_reviews', '=', 0)
		//echo count($models);
		//die();
        foreach ($models as $model) {
            $reviews = DB::select()->from('yandexmarket_reviews')->where('model_id', '=', $model->id)->execute()->as_array();;
            foreach ($reviews as $review) {
                $model->change_count_stars($review['rating']);
            }
			if (count($reviews)) {
				$model->count_reviews = count($reviews);
				$model->save();
			}
        }
		echo '312312';
    }
    
    public function action_test19()
    {
        set_time_limit(0);
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if (!$param) {
            die();    
        }
        list($shop_id, $category_id) = explode('-', $param);
        if (!$shop_id) {
            die();    
        }
        if (!$category_id) {
            $category_id = null;
        }
        $shop = ORM::factory('shop', $shop_id);
        $shop->set_as_current();
        
        $products = ORM::factory('product')->get_products($category_id, 0); 
        $count = $count_found = 0;
        foreach ($products as $product) {
            $count++;
            echo $count.' ('.$product->id.') - '.$product->name;
            
            $models = ORM::factory('model')->search($product->clean_name, 5);
            if ($models) {
                foreach ($models as $model) {
                    //echo '<br />'.$product->get_clean_name().' - '.$model->get_clean_name();
                    echo '<br /><span style="color:green;">'.$model->name.'</span> ('.$model->count_reviews.') <a href="/index/test/to_model/'.$product->id.'-'.$model->id.'" target="_blank">привязать</a>';
                    if ((strstr($product->get_clean_name(), $model->get_clean_name()) !== false ||
                            strstr($model->get_clean_name(), $product->get_clean_name()) !== false)) {
                        $product->to_model($model);
                        $count_found++;
                        echo '<br /><span style="color:red;">В '.$model->name.'</span><br />';
                        break;        
                    }    
                }
                echo '<br /><br />'; 
            }
        }
        echo '<br /><br />Всего - '.count($products).'<br />'.
            'Нашлось наших моделей - '.$count_found.'<br />';
    }
    
    public function action_to_model()
    {
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if ($param) {
            list($product_id, $model_id) = explode('-', $param);
            $product = ORM::factory('product', $product_id);
            $model   = ORM::factory('model', $model_id);
            $product->to_model($model);
        }
    }
    
    // Модели с отзовика
    public function action_test20()
    {  
        set_time_limit(0);     
        $category_id  = 2;
        $category_url = 'http://www.otzovik.com/auto/autospares/tires/';
        
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('<a class=\'npage\' title=\'Следующая\' href=\'', '\'', $data)) {
                return 'http://www.otzovik.com'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str("<td style='padding: 5px' class=imagesel", "</tr>", $data)) {
                return $_data;
            } 
            return null; 
        } 
        
        function get_model_link($data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str("<a href=", ">", $data)) {
                return 'http://www.otzovik.com'.$link;
            }  
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str("<td class=nprods>", "</td>", $data)) {
                return strip_tags($name);
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str(" src=", " width", $data)) {
                if ($image != 'http://i.otzovik.com/nophoto_m.png') {
                    return $image;      
                }
            }           
            return null;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str(">Отзывы:", "</a>", $data)) {
                return (int)trim($count);
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $i++; 
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);    
                $count_reviews  = get_model_count_reviews($model_data);
                //DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_reviews'))
                //    ->values(array($category_id, $name, $link, $image, $count_reviews))->execute();              
                echo  $i . ' - ' . $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_reviews . '<br />';
                    
            }
            echo $category_url . '<br />';

            usleep(700000);
            //sleep(1);
            
        } while ($category_url = get_next_page_url($data));
        
    }
    
    public function action_test23()
    {
        //die();
        set_time_limit(0); 
        $categories = array(1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967,);
        $products = DB::select()
                ->from('tmp_models_3')
                ->where('category_id', 'IN', $categories)
                ->and_where('model_id', 'IS', NULL)
                ->execute()
                ->as_array();
        foreach ($products as $product) {
			try {
				$model = DB::insert('models', array('name', 'tmp_category_id_3', 'potential_count_reviews_3', 'irecommend_url'))
						->values(array($product['name'], $product['category_id'], $product['count_reviews'], $product['link']))->execute();
				DB::update('tmp_models_3')->value('model_id', $model[0])->where('id', '=', $product['id'])->execute();        
			} catch (Database_Exception $e) {
				if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
					continue;
				}
				die($e->getMessage());
			}
        }    
        echo count($products); 
    } 
    
    public function action_test21()
    {	
		//die();
        set_time_limit(0); 
        $categories = array(1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967,); 
        $products = DB::select()
                ->from('tmp_models_3')
                ->where('category_id', 'IN', $categories)
                //->and_where('model_id', 'IS', NULL)
                //->offset(0)
                //->limit(1000)
                ->execute()
                ->as_array(); 
        $count_found = $count = 0;
		
		function _get_clean_name($name) {
			return $name;
			// TODO Если есть слово "для", то оставляем все как есть (???)
			
			$name = _cut_trash($name);
			$words = _get_words($name);
			
			$morphy = Morphy::instance();
			$result = array();
			foreach ($words as $word) {
				$word = mb_strtoupper(trim($word));
				if (!$word) {
					continue;
				}
				
				// Если не нашли, значит либо название товара, либо еще что-то
				// Сохраняем, а русские слова удаляем
				if (!($part_of_speech = $morphy->getPartOfSpeech($word))) {
					$result[] = $word;         
				}   
			}
			
			$result = implode(' ', $result);
			if (!trim($result)) {
				return 'Dfsdfsdw43 324 342';
			}
			return $result;
		}
		
		function _cut_trash($name) {
			// Вырезаем содержимое скобок - (серый)
			// Чаще всего там мусор или цвет
			$name = _cut_brackets($name); 
			
			// TODO Вырезать " и &quot (???)
			//$name = $this->_cut_quotes($name); 
			
			// TODO Удалять все, что справа "+"      
			
			return trim($name);
		}
		
		function _cut_brackets($name, $cut_single_bracket = true) {
			$result = preg_replace('/(\([^\)]+\))/i', '', $name);
			if ($cut_single_bracket) {
				$result = preg_replace('/(\([^\)]+)/i', '', $result);    
			}
			return $result;   
		}
		
		function _get_words($name) {
			$words = explode(' ', $name);
			
			// Удаляем запятые
			foreach ($words as &$word) {
				$word = str_replace(',', '', $word);    
			}
			
			return $words;
		}
		
        foreach ($products as $product) {
            $count++;
            echo $count.' ('.$product['id'].') - '.$product['name'];
            
            $found = false;
            $models = ORM::factory('model')->search($product['name'], 15);
            if ($models) {
                foreach ($models as $model) {
                    //echo '<br />'.$product->get_clean_name().' - '.$model->get_clean_name();
                    echo '<br /><span style="color:green;">'.$model->name.'</span> ('.$model->count_reviews.') <a href="/index/test/test22/'.$product['id'].'-'.$model->id.'" target="_blank">привязать</a>';
					if ((strstr(Model_Main::get_clean_name(_get_clean_name($product['name'])), $model->get_clean_name()) !== false ||
                            strstr($model->get_clean_name(), Model_Main::get_clean_name(_get_clean_name($product['name']))) !== false)) {
						if (!$model->irecommend_url) {
							$model->irecommend_url = $product['link'];
							$model->potential_count_reviews_3 = $product['count_reviews'];
							$model->save();
						}
						DB::update('tmp_models_3')->value('model_id', $model->id)->where('id', '=', $product['id'])->execute();
                        
                        $count_found++;
                        $found = true;
                        echo '<br /><span style="color:red;">#'.$model->id.' '.$model->name.'</span>';
                        break;        
                    }    
                } 
            }
            echo '<br /><br />';
        }
        echo '<br /><br />Всего - '.count($products).'<br />'.
            'Нашлось наших моделей - '.$count_found.'<br />';
    }
    
    public function action_test22()
    {
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if ($param) {
            list($product_id, $model_id) = explode('-', $param);
            $product = DB::select()->from('tmp_models_3')->where('id', '=', $product_id)->execute()->as_array();
            $model   = ORM::factory('model', $model_id);
            $product = $product[0];
 
            $model->irecommend_url = $product['link'];
            $model->potential_count_reviews_3 = $product['count_reviews'];
            $model->save();
            DB::update('tmp_models_3')->value('model_id', $model->id)->where('id', '=', $product['id'])->execute();   
        }
    }    
	
	// Модели с айрекомменд
    public function action_test24()
    {  
        set_time_limit(0);     
        $category_id  = 4;
        $category_url = 'http://www.irecommend.ru/category/konditsionery?tid=77028';
        
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('class="pager-next"><a href="', '"', $data)) {
				$url = str_replace('&amp;', '&', $url);
                return 'http://www.irecommend.ru'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str("<div class=\"block\">", "<!-- /end node.tpl.php -->", $data)) {
                return $_data;
            } 
            return null; 
        } 
        
        function get_model_link(&$data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str("class=\"title\"><a href=\"", "\">", $data)) {
                return 'http://www.irecommend.ru'.$link;
            }  
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str("<h2 class=\"title\">", "</h2", $data)) {
                return trim(strip_tags($name));
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str(" src=\"", "\"", $data)) {
                //if ($image != 'http://i.otzovik.com/nophoto_m.png') {
                    return $image;      
                //}
            }           
            return null;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str("\">Читать отзывы (", ")</a", $data)) {
                return (int)trim($count);
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $i++; 
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);    
                $count_reviews  = get_model_count_reviews($model_data);
                //DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_reviews'))
                //    ->values(array($category_id, $name, $link, $image, $count_reviews))->execute();              
                echo  $i . ' - ' . $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_reviews . '<br />';
                    
            }
            echo $category_url . '<br />';

            usleep(700000);
            //sleep(1);
            
        } while ($category_url = get_next_page_url($data));
        
    }
	
	public function action_test25()
    {
        die();
        set_time_limit(0); 
        $models = DB::select()
                ->from('models')
				->offset(100000)
                ->limit(100000)
                ->execute()
                ->as_array();
        foreach ($models as $model) {
			if ($model['market_url']) {
				$products = DB::select()
					->from('tmp_models')
					->where('reviews_link', '=', $model['market_url'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('yandexmarket_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
			if ($model['otzovik_urls']) {
				$products = DB::select()
					->from('tmp_models_2')
					->where('model_id', '=', $model['id'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('otzovik_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
			if ($model['irecommend_url']) {
				$products = DB::select()
					->from('tmp_models_3')
					->where('link', '=', $model['irecommend_url'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('irecommend_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
        }    
        echo count($models); 
    } 
	
	public function action_test26()
    {
        die();
        set_time_limit(0); 
        $categories = DB::select()
                ->from('categories')
                ->execute()
                ->as_array();
        foreach ($categories as $category) {
			if (!$category['yandexmarket_categories'] && !$category['otzovik_categories'] &&
					!$category['irecommend_categories']) {
				continue;
			}
		
			$query = DB::select()
                ->from('models');
			
			$first = true;	
			if ($category['yandexmarket_categories']) {
				$query->where('yandexmarket_category', 'IN', explode(',', $category['yandexmarket_categories']));
				$first = false;
			}		
			if ($category['otzovik_categories']) {
				if ($first) {
					$query->where('otzovik_category', 'IN', explode(',', $category['otzovik_categories']));
				} else {
					$query->or_where('otzovik_category', 'IN', explode(',', $category['otzovik_categories']));
				}				
			}		
			if ($category['irecommend_categories']) {
				if ($first) {
					$query->where('irecommend_category', 'IN', explode(',', $category['irecommend_categories']));
				} else {
					$query->or_where('irecommend_category', 'IN', explode(',', $category['irecommend_categories']));
				}
			}
				
			$models = $query
                ->execute()
                ->as_array();
				
			foreach ($models as $model) {
				DB::update('models')->value('category_id', $category['id'])->where('id', '=', $model['id'])->execute();
			}
				
			echo count($models).'<br />';
        }    
        echo count($models); 
		
		
		//INSERT INTO shop_categories (source_id, parent_id, shop_id, name)
		//SELECT id, parent_id, 3 as shop_id, name FROM `categories_3`
    }
    
    public function action_test27()
    {
        //header('Content-type: text/html; charset=UTF-8');
        /*$ids = array();
        for($i=0; $i<10000; $i++){
            $ids[] = mt_rand(1, 381684);
        }*/
        //file_put_contents(DOCROOT.'data/ids.txt',implode("\n", $ids),FILE_APPEND);
        //die();
        
        //$ids = file(DOCROOT.'data/ids.txt');
        /*$start = 0; $end = 1000;
        while(1){
            echo $start . " - " . $end . "<br>";
            $query = DB::select()
                    ->from('goods');
            $query->where('id', '>=', $start)->and_where('id', '<', $end)->order_by('id', 'ASC');
            $names = $query
                    ->execute()
                    ->as_array();
            if(count($names) == 0){
                break;
            }
            $words = array();
            foreach($names as $name){
                //echo $name['name'] . "<br>";
                $name = mb_strtolower($name['name']);
                //echo $name . "<br>";
                $name = html_entity_decode($name, ENT_QUOTES, "UTF-8");
                //echo $name . "<br>";
                
                $name = strip_tags($name);
                //echo $name . "<br>";
                $name = preg_replace('/[a-zа-яё0-9-]+\./iu', ' ', $name);
                $name = preg_replace('/[^a-zа-яё0-9- ]+/iu', ' ', $name);
                $name = preg_replace('/[0-9-]+/iu', ' ', $name);
                //$name = preg_replace('/ [0-9]+ /iu', ' ', $name);
                //echo $name . "<br>";
                $name = preg_replace('/ [0-9]+( |$)/iu', ' ', $name);
                //$name = preg_replace('/ [a-zа-яё]( |$)/iu', ' ', $name);
                
                //$name = preg_replace('/( [a-zа-яё] [a-zа-яё] | [a-zа-яё] | [a-zа-яё]$)/iu', ' ', $name);
                //$name = preg_replace('/([ ]+| (-)+)/iu', ' ', $name);
                $name = trim($name);
                //echo $name . "<br>";
                
                
                $tmp = explode(" ", $name);
                foreach($tmp as $w){
                    if($w != "" && mb_strlen($w) > 3)
                        $words[$w] = mb_strlen($w);
                } 
                //break;
            }
            
            $words_in = array();
            
            foreach($words as $key => $w){
                $query = DB::insert('words', array('word'));
                $query->values(array($key));    
            
            try {
                    $query->execute();
                    usleep(1000);
                } catch (Database_Exception $e) {
                    if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
                        //echo $e->getMessage();
                    } else {
                        die($e->getMessage());
                    }
                }
            }
            //sleep(1);
            $start = $end; 
            $end = $end + 1000;
        }*/
        //print_r($words);
        //echo count($words);
        //die();
        $offset = 0;
        while(1){
        echo $offset . "<br>";
        //die();
        $query = DB::select()
                ->from('words');
        $query->where('wordstat','=', NULL)->order_by('id', 'ASC');//->limit(50)->offset($offset);
        $names = $query
                ->execute()
                ->as_array();
        if(count($names) == 0){
            break;
        }
    
        //$word_request = array();
        foreach($names as $name){
            file_put_contents(DOCROOT.'data/names.txt',$name['id'].";".$name['word']."\n",FILE_APPEND);
            continue;
            usleep(300000);
            //$word_request[] = $key;
            //break;    
            
            //echo "<pre>";
            //print_r($name);
            //die();
            
            $url = 'http://api.apishops.com/api';
            
            $data = array('action' => 'getWordstat', 'login' => 'Evgeny1397', 'password' => md5("GtrFvb123"), 'phrase[]' => $name['word'], 'type' => 2);
            //print_r($data);
            // use key 'http' even if you send the request to https://...
            $options = array(
                'http' => array(
                    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method'  => 'POST',
                    'content' => http_build_query($data),
                ),
            );
            //echo http_build_query($data);
            $context  = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $data = json_decode($result);
            //var_dump($result);
            //echo "<pre>";
            if($data->items[0]->state != "ready"){
                print_r($data);    
            }
            //print_r($data);
            //break;
        
            //print_r($data->items[0]->state);
            //foreach($data->items as $item){
            if($data->items[0]->state == "ready"){
                try {
                /*DB::insert('words', array('word', 'min_price', 'max_price', 'wordstat', 'first_place_clicks'))->values(
                array($data->items[0]->phrase, $data->items[0]->cpcMin, $data->items[0]->cpcMax, $data->items[0]->wordstat, $data->items[0]->firstPlaceClicks))->execute();*/
                $query = DB::update('words')
                            ->set(array('min_price' => $data->items[0]->cpcMin, 'max_price' => $data->items[0]->cpcMax,
                                        'wordstat' => $data->items[0]->wordstat, 'first_place_clicks' => $data->items[0]->firstPlaceClicks))->
                                        where('id', '=', $name['id'])->execute();
                } catch (Database_Exception $e) {
                    if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
                        
                    } else {
                        echo $e->getMessage();
                    }
                }
            }
            //die();
        }
            $offset = $offset + 50;
            die();    
        }
        //die();
        //}
        /*echo "<pre>";
        print_r($words);
        echo count($words);
        die();*/        
        //Model_SupplierParser_Optlist::instance()->parse();
    }
    
    public function action_test28()
    {
        //Model_SupplierParser_Optlist::instance()->parse_goods();
        header('Content-type: text/html; charset=UTF-8');
        //$offset = 0;
        while(1){
            $query = DB::select()->from('goods');
            $query->where('kei','=',NULL)->order_by('id', 'ASC')->limit(100);
            $names = $query
                    ->execute()
                    ->as_array();
            if(count($names) == 0){
                break;
            }
            foreach($names as $name){
                //echo "<pre>";
                //print_r($name);
                //die();
                
                $words = Model_Supplier_Optlist::get_words_name($name['name']);
                //print_r($words);
                //die();
                if(count($words) > 0){
                    $words_data = DB::select()->from('words')->where('word','IN',$words)->and_where('kei','>', 0)->execute()->as_array();
                    
                    //print_r($words_data);
                    //die();
                    if(count($words_data) > 0){
                        $kei_sum = 0;
                        foreach($words_data as $row){
                            $kei_sum += $row['kei'];    
                        }
                        $kei_goods = round($kei_sum / count($words_data), 3);
                    } else {
                        $kei_goods = 0;
                    }
                } else {
                    $kei_goods = 0;
                }
                    $query = DB::update('goods')
                                    ->set(array('kei' => $kei_goods))->
                                                where('id', '=', $name['id'])->execute();
                    //echo $kei_goods;
                    //break;
                
            }
            
            //break;
            //$offset += 100;
            //sleep(5);
        }
    }
    
    public function action_test29()
    {
        $query = DB::select()->from("words")->/*where('kei','=',NULL)->*/order_by('id', 'ASC');
        $data = $query
                ->execute()
                ->as_array();
        foreach($data as $row){
            if($row['wordstat'] && $row['min_price'] && $row['max_price']){
                $P = $row['wordstat'];
                $U = (($row['max_price'] + $row['min_price']) / 2) * 100;
                $kei = round($P * 1000 / $U, 3);
            } else {
                $kei = 0;
            }
            //echo "<pre>";
            //print_r($row);
            //echo $kei;
            $query = DB::update('words')
                            ->set(array('kei' => $kei))->
                                        where('id', '=', $row['id'])->execute();
        }
        echo "OK!";
    }
    
    public function action_test30()
    {
        $data = file(DOCROOT.'data/out2.txt');
        
        foreach($data as $word_data){
            if($word_data){
                $word_data_arr = explode(";", $word_data);
                $query = DB::update('words')
                            ->set(array('min_price' => $word_data_arr[2], 'max_price' => $word_data_arr[3],
                                        'wordstat' => $word_data_arr[4], 'first_place_clicks' => $word_data_arr[5]))->
                                        where('id', '=', $word_data_arr[0])->execute();
            }
            
        }
    }
    
    public function action_test31()
    {
        Model_SupplierParser_Optlist::instance()->parse_payment_details();
    }
    
    public function action_test32()
    {
        //Model_Supplier_Optlist::instance()->add_email();
        $data = file(DOCROOT.'data/bd3.txt');
        foreach($data as $word_data){
            if($word_data){
                $word_data_arr = explode(";", $word_data);
                    if(trim($word_data_arr[1])){
                    $query = DB::update('suppliers')
                                ->set(array('email' => trim($word_data_arr[1])))->
                                            where('id', '=', $word_data_arr[0])->execute();
                }
            }
            
        }
    }
    
    public function action_test33() 
    {
        set_time_limit(0);
        die();
        
        $needed_ids = array(596, 2588, 1948, 1892, 2070, 723, 1134);
        
        $goods = new Model_Goods_Optlist();
        
        $keys = file(DOCROOT.'1.txt');
        $keys = array_slice($keys, 10000, 100000);
        foreach($keys as $key){
             list($key, $stat) = explode(':', $key);
             $search_result = $goods->search($key, null, true);
             
             if ($search_result['result']) {
                foreach ($search_result['result'] as $item) {
                    if (in_array($item['id'], $needed_ids)) {
                        file_put_contents('2.txt', $key."\t\t".$stat, FILE_APPEND);
                        break;    
                    }   
                }    
             }
        }
        echo count($keys);  
    } 
    
    public function action_test34() 
    {
        set_time_limit(0);
        
        $needed_ids = array(596, 2588, 1948, 1892, 2070, 723, 1134);
        
        $goods = new Model_Goods_Optlist();
        
        $keys = file(DOCROOT.'2.txt');
        foreach($keys as $key){
             list($key, $stat) = explode("\t\t", $key);
             $search_result = $goods->search($key, null, true);
             
             $isset_suppliers = array();
             if ($search_result['result']) {
                foreach ($search_result['result'] as $item) {
                    if (in_array($item['id'], $needed_ids)) {
                        $isset_suppliers[] = $item['id'];    
                    }   
                }    
             }
             file_put_contents('3.txt', $key.":".trim($stat).":".implode(',', $isset_suppliers)."\r\n", FILE_APPEND);
        }
        echo count($keys);  
    }    
	
	public function action_test35() 
    {
        set_time_limit(0);
        die();
        
        $goods = new Model_Goods_Optlist();
        
        $keys = file(DOCROOT.'4.txt');
        //$keys = array_slice($keys, 0, 10);
        foreach($keys as $key){
			 $key = trim($key);
             $search_result = $goods->search($key, null, true);
			 
             $founded = array();
             if ($search_result['result']) {
                $search_result['result'] = array_slice($search_result['result'], 0, 30);
                foreach ($search_result['result'] as $item) {
                    $founded[] = $item['id'];
                }    
             }
             
             $ids = $product_ids = array();
             foreach ($founded as $id) {
                if ($id > 1000000) {
                    $id -= 1000000;
                    $product_ids[] = $id;   
                } else {
                    $ids[$id] = $id;    
                }   
             }
             
             $products = array();
             if (count($product_ids)) {
                $products = ORM::factory('goods_optlist')
                                    ->where('id', 'IN', $product_ids)
                                    //->where('is_available', '=', 1)
                                    //->order_by('price', 'ASC')
                                    ->find_all()
                                    ->as_array();
             }
             foreach ($products as $product) {
                $ids[$product->supplier_id] = $product->supplier_id;   
             }
             
             $suppliers = ORM::factory('supplier_optlist')
                                    ->where('id', 'IN', $ids)
                                    ->find_all()
                                    ->as_array();
             foreach ($suppliers as $supplier) {
                file_put_contents('5.txt', $supplier->id.":".$supplier->name.":".$supplier->phone."\r\n", FILE_APPEND);   
             }
        }
        echo count($keys);  
    }  
    
    public function action_test36() 
    {
        set_time_limit(0);
        
        $goods = new Model_Goods_Optlist();
        
        $ids = array();
        
        $keys = file(DOCROOT.'6.txt');
        //$keys = array_slice($keys, 0, 10);
        foreach($keys as $key){
             $key = trim($key);
             list($key, $tmp) = explode("\t", $key);
             $search_result = $goods->search($key, 1);
             echo $key.' - '.$search_result['count'].'<br />';
             $search_result = $search_result['result'];
             $search_result2 = $goods->search($key, 2); 
             $search_result = array_merge($search_result, $search_result2['result']);
             
             foreach ($search_result as $item) {
                if ($item instanceof Model_Goods_Optlist) {
                    $ids[$item->supplier_id] = $item->supplier_id;    
                } 
                if ($item instanceof Model_Supplier_Optlist) {
                    $ids[$item->id] = $item->id;   
                }  
             }
        }
        echo '<hr />';
        foreach ($ids as $id) {
            echo $id.'<br />';    
        }  
    }    
    
    public function action_test37() 
    {
        set_time_limit(0);
        
        $keys = file(DOCROOT.'tmp\all.txt');
        //$keys = array_slice($keys, 0, 10);
        $count_less40 = 0;
        $count_null = 0;
        foreach($keys as $key){
             $key = trim($key);
             list($key, $tmp) = explode("\t", $key);
             $tmp = str_replace(' ', '', $tmp);
             if ($tmp < 40) {
                $count_less40++;
                continue;    
             }
             $search_result = ORM::factory('good')->search($key, null, true);
             if ($search_result['count']) {
                echo $key.':'.$tmp.':'.$search_result['count'].'<br />';
             } else {
                $count_null++; 
             }  
             //break;
        }  
        
        echo '<br /><br />';
        echo 'all - '.count($keys).'<br />';
        echo 'null - '.$count_null.'<br />'; 
        echo 'less40 - '.$count_less40.'<br />';
    }    
	
	public function action_test38() 
    {
        set_time_limit(0);
		
		$url  = 'http://tiu.ru/Rukodelie;wholesale';
		$html = Model_SupplierParser_Abstract::_get_source_code($url);
		$html = Model_SupplierParser_Abstract::_cut_str('b-facet__body b-facet__body_type_categories h-ml-10 h-mt-10', '<div class="b-facet__body">', $html);
		
		/*while ($link = Model_SupplierParser_Abstract::_cut_str('b-categories-top__item-wrapper">', '</div>', $html)) {
			$href = 'http://tiu.ru'.Model_SupplierParser_Abstract::_cut_str('href="', '"', $link);
            $name = trim(Model_SupplierParser_Abstract::_cut_str('>', '<', $link));
            echo $name."***".$href.'<br />';    
        } */
		
		while ($link = Model_SupplierParser_Abstract::_cut_str('b-facet__value-link"', '/a>', $html)) {
			$href = 'http://tiu.ru'.Model_SupplierParser_Abstract::_cut_str('href="', '"', $link);
            $name = trim(Model_SupplierParser_Abstract::_cut_str('>', '<', $link));
            echo $name."***".$href.'<br />';    
        }
	
	}
	
	public function action_test39() 
    {
        set_time_limit(0);
		
		function get_categories($url, &$array, &$current_id, $parent_id) {
			$page = Model_SupplierParser_Abstract::_get_source_code($url);
			sleep(2);
			$html = Model_SupplierParser_Abstract::_cut_str('b-facet__body b-facet__body_type_categories h-ml-10 h-mt-10', '<div class="b-facet__body">', $page);
			if ($html) {
				while ($link = Model_SupplierParser_Abstract::_cut_str('b-facet__value-link"', '/a>', $html)) {
					$current_id++;
				
					$href = 'http://tiu.ru'.Model_SupplierParser_Abstract::_cut_str('href="', '"', $link);
					$name = trim(Model_SupplierParser_Abstract::_cut_str('>', '<', $link));
					$array[$current_id] = array(
						'href'      => $href,
						'name'      => $name,
						'parent_id' => $parent_id,
						'items'     => array(),
					); 
					get_categories($href, $array[$current_id]['items'], $current_id, $current_id);
				}
			} else {
				if (strstr($page, 'id="drugie-regiony"') !== false) {
					$html = Model_SupplierParser_Abstract::_cut_str('qa-category-top-side-bar-block', 'id="drugie-regiony"', $page);
				} else {
					$html = Model_SupplierParser_Abstract::_cut_str('qa-category-top-side-bar-block', '<div class="b-category-tile">', $page);
				}
				
				while ($link = Model_SupplierParser_Abstract::_cut_str('b-categories-top__item-wrapper">', '</div>', $html)) {
					$current_id++;
				
					$href = 'http://tiu.ru'.Model_SupplierParser_Abstract::_cut_str('href="', '"', $link);
					$name = trim(Model_SupplierParser_Abstract::_cut_str('>', '<', $link));
					$array[$current_id] = array(
						'href'      => $href,
						'name'      => $name,
						'parent_id' => $parent_id,
						'items'     => array(),
					); 
					get_categories($href, $array[$current_id]['items'], $current_id, $current_id);
				}
			}
		}
		
		function print_categories($data, $level = 0) {
			foreach ($data as $id => $item) {
				for ($i = 0; $i < $level; $i++) {
					echo "\t";
				}
				echo $id."\t".$item['parent_id']."\t".$item['name']."\t".$item['href']."\r\n";
				if (count($item['items'])) {
					print_categories($item['items'], $level + 1);
				}
			}
		}
		//die('ds');
		$data = array();
		$current_id = 10492;
		get_categories('http://tiu.ru/Vse-dlya-otpuska-2;wholesale', $data, $current_id, 34);
		print_categories($data, 0);
		//echo '<pre>';
		//print_r($data);

	
	}
	
	public function action_test40() 
    {
        set_time_limit(0);
		
		$lines = file('7.txt');
		$founded = array();
		foreach ($lines as $line) {
			$line = trim($line);
			if (!$line) {
				continue;
			}
			list($id, $parent_id, $name, $url) = explode("\t", $line);
			
			$found = false;
			foreach ($lines as $_line) {
				$_line = trim($_line);
				if (!$_line) {
					continue;
				}
				list($_id, $_parent_id, $_name, $_url) = explode("\t", $_line);
				
				if ($url == $_url && $id != $_id && !isset($founded[$id]) && !isset($founded[$_id])) {
					$found = true;
					$founded[$id] = 1;
					$founded[$_id] = 1;
					break;
				}
			}
			
			echo "{$id}\t{$parent_id}\t{$name}\t{$url}\t";
			if ($found) {
				echo $_id;
			} else {
				echo '0';
			}
			echo "\r\n";
		}
	}
	
	// http://tiu.ru/Vintovye-fundamentnye-svai
	// http://tiu.ru/Sportivnye-stenki;wholesale?per_page=96	
	public function action_test41() 
    {
        set_time_limit(0);
		
		Model_ProductParser_Tiu::instance()->parse();
	}
	
	public function action_test42() 
    {
		//die();
        set_time_limit(0);
		// 30,9838,9918,9993,10101,10133,10196,10230,10289,10298,9839,9840,9850,9864,9865,9877,9878,9885,9901,9902,9903,9904,9905,9906,9907,9908,9909,9910,9911,9912,9913,9914,9915,9916,9917,9919,9931,9943,9947,9956,9967,9968,9969,9970,9974,9975,9976,9977,9978,9979,9980,9981,9982,9983,9984,9985,9986,9987,9988,9989,9990,9991,9992,9994,10016,10031,10044,10054,10055,10069,10082,10083,10084,10092,10099,10100,10102,10103,10113,10122,10123,10124,10125,10126,10127,10128,10129,10130,10131,10132,10134,10153,10154,10165,10166,10167,10168,10169,10170,10171,10172,10173,10174,10179,10180,10181,10182,10183,10184,10185,10186,10187,10188,10189,10190,10191,10192,10193,10194,10195,10197,10198,10206,10207,10215,10216,10217,10218,10222,10223,10224,10225,10226,10227,10228,10229,10231,10247,10266,10276,10288,10290,10291,10292,10293,10294,10295,10296,10297,9841,9842,9843,9844,9845,9846,9847,9848,9849,9851,9852,9853,9854,9855,9856,9857,9858,9859,9860,9861,9862,9863,9866,9867,9868,9869,9870,9871,9872,9873,9874,9875,9876,9879,9880,9881,9882,9883,9884,9886,9887,9888,9889,9890,9891,9892,9893,9894,9895,9896,9897,9898,9899,9900,9920,9921,9922,9923,9924,9925,9926,9927,9928,9929,9930,9932,9933,9934,9935,9936,9937,9938,9939,9940,9941,9942,9944,9945,9946,9948,9949,9950,9951,9952,9953,9954,9955,9957,9958,9959,9960,9961,9962,9963,9964,9965,9966,9971,9972,9973,9995,9996,9997,9998,9999,10000,10001,10002,10003,10004,10005,10006,10007,10008,10009,10010,10011,10012,10013,10014,10015,10017,10018,10019,10020,10021,10022,10023,10024,10025,10026,10027,10028,10029,10030,10032,10033,10034,10035,10036,10037,10038,10039,10040,10041,10042,10043,10045,10046,10047,10048,10049,10050,10051,10052,10053,10056,10057,10058,10059,10060,10061,10062,10063,10064,10065,10066,10067,10068,10070,10071,10072,10073,10074,10075,10076,10077,10078,10079,10080,10081,10085,10086,10087,10088,10089,10090,10091,10093,10094,10095,10096,10097,10098,10104,10105,10106,10107,10108,10109,10110,10111,10112,10114,10115,10116,10117,10118,10119,10120,10121,10135,10136,10137,10138,10139,10140,10141,10142,10143,10144,10145,10146,10147,10148,10149,10150,10151,10152,10155,10156,10157,10158,10159,10160,10161,10162,10163,10164,10175,10176,10177,10178,10199,10200,10201,10202,10203,10204,10205,10208,10209,10210,10211,10212,10213,10214,10219,10220,10221,10232,10233,10234,10235,10236,10237,10238,10239,10240,10241,10242,10243,10244,10245,10246,10248,10249,10250,10251,10252,10253,10254,10255,10256,10257,10258,10259,10260,10261,10262,10263,10264,10265,10267,10268,10269,10270,10271,10272,10273,10274,10275,10277,10278,10279,10280,10281,10282,10283,10284,10285,10286,10287
		
		// потом связи с категориями обработать еще отдельно
		
		//$categories = array(30,9838,9918,9993,10101,10133,10196,10230,10289,10298,9839,9840,9850,9864,9865,9877,9878,9885,9901,9902,9903,9904,9905,9906,9907,9908,9909,9910,9911,9912,9913,9914,9915,9916,9917,9919,9931,9943,9947,9956,9967,9968,9969,9970,9974,9975,9976,9977,9978,9979,9980,9981,9982,9983,9984,9985,9986,9987,9988,9989,9990,9991,9992,9994,10016,10031,10044,10054,10055,10069,10082,10083,10084,10092,10099,10100,10102,10103,10113,10122,10123,10124,10125,10126,10127,10128,10129,10130,10131,10132,10134,10153,10154,10165,10166,10167,10168,10169,10170,10171,10172,10173,10174,10179,10180,10181,10182,10183,10184,10185,10186,10187,10188,10189,10190,10191,10192,10193,10194,10195,10197,10198,10206,10207,10215,10216,10217,10218,10222,10223,10224,10225,10226,10227,10228,10229,10231,10247,10266,10276,10288,10290,10291,10292,10293,10294,10295,10296,10297,9841,9842,9843,9844,9845,9846,9847,9848,9849,9851,9852,9853,9854,9855,9856,9857,9858,9859,9860,9861,9862,9863,9866,9867,9868,9869,9870,9871,9872,9873,9874,9875,9876,9879,9880,9881,9882,9883,9884,9886,9887,9888,9889,9890,9891,9892,9893,9894,9895,9896,9897,9898,9899,9900,9920,9921,9922,9923,9924,9925,9926,9927,9928,9929,9930,9932,9933,9934,9935,9936,9937,9938,9939,9940,9941,9942,9944,9945,9946,9948,9949,9950,9951,9952,9953,9954,9955,9957,9958,9959,9960,9961,9962,9963,9964,9965,9966,9971,9972,9973,9995,9996,9997,9998,9999,10000,10001,10002,10003,10004,10005,10006,10007,10008,10009,10010,10011,10012,10013,10014,10015,10017,10018,10019,10020,10021,10022,10023,10024,10025,10026,10027,10028,10029,10030,10032,10033,10034,10035,10036,10037,10038,10039,10040,10041,10042,10043,10045,10046,10047,10048,10049,10050,10051,10052,10053,10056,10057,10058,10059,10060,10061,10062,10063,10064,10065,10066,10067,10068,10070,10071,10072,10073,10074,10075,10076,10077,10078,10079,10080,10081,10085,10086,10087,10088,10089,10090,10091,10093,10094,10095,10096,10097,10098,10104,10105,10106,10107,10108,10109,10110,10111,10112,10114,10115,10116,10117,10118,10119,10120,10121,10135,10136,10137,10138,10139,10140,10141,10142,10143,10144,10145,10146,10147,10148,10149,10150,10151,10152,10155,10156,10157,10158,10159,10160,10161,10162,10163,10164,10175,10176,10177,10178,10199,10200,10201,10202,10203,10204,10205,10208,10209,10210,10211,10212,10213,10214,10219,10220,10221,10232,10233,10234,10235,10236,10237,10238,10239,10240,10241,10242,10243,10244,10245,10246,10248,10249,10250,10251,10252,10253,10254,10255,10256,10257,10258,10259,10260,10261,10262,10263,10264,10265,10267,10268,10269,10270,10271,10272,10273,10274,10275,10277,10278,10279,10280,10281,10282,10283,10284,10285,10286,10287);
		$goods = DB::select('original_supplier_id', 'url')
					->from('goods2')
					->where('supplier_id', '=', 0)
					->group_by('original_supplier_id')
					->execute()
					->as_array(); 
		//echo '<pre>';
		//print_r($goods);
		foreach ($goods as $good) {
			echo $good['original_supplier_id']." => '".$good['url']."',<br />";
		}
		
		// ALTER TABLE  `goods2` ADD  `supplier_id` INT( 11 ) NOT NULL DEFAULT  '0' AFTER  `original_supplier_id`;
		
		/*$parents = array(9841);
		$categories = DB::select('id')
					->from('categories2')
					->where('parent_id', 'IN', $parents)
					->execute()
					->as_array(); 
		foreach ($categories as $category) {
			echo $category['id'].',';
		}*/
	}
	
	public function action_test43() 
    {
		set_time_limit(0);
		die();
	
		// http://moskva.tiu.ru/p47987467-kuoma-patriot;wholesale.html
		// http://chelny.tiu.ru/p35008806-sapogi-zimnie;wholesale.html
		$categories = array(
		);
		
		foreach ($categories as $original_supplier_id => $url) {
			echo $original_supplier_id.' - '.$url.'<br />';
		
			$parts = explode('/', $url);
			$url  = 'http://tiu.ru/'.$parts[count($parts)-1];
			$html = Model_SupplierParser_Abstract::_get_source_code($url);
			
			if (strstr($html, 'Защита от роботов') !== false) {
				die('ban');
			}
			
			// Адрес сайта
			$site_url = Model_SupplierParser_Abstract::_cut_str('<div class="h-mb-5 b-text-hider b-text-hider_type_max-two-lines', '</div>', $html);
			$site_url = Model_SupplierParser_Abstract::_cut_str('<a href="', '"', $site_url);

			$site_url = $site_url.'contacts';
			//echo $site_url;
			
			// График работы
			$schedule = array(
				'items'  => array(),
				'remark' => null,
			);
			$block = Model_SupplierParser_Abstract::_cut_str('<div class="b-schedule">', '</div>', $html);
			$first = false;
			while ($tr = Model_SupplierParser_Abstract::_cut_str('<tr>', '</tr>', $block)) {
				if (!$first) {
					$first = true;
					continue;
				}
				$day  = Model_SupplierParser_Abstract::_cut_str('<td class="b-schedule__cell">', '</td>', $tr);
				$time = Model_SupplierParser_Abstract::_cut_str('<td class="b-schedule__cell">', '</td>', $tr);
				$rest = Model_SupplierParser_Abstract::_cut_str('<td class="b-schedule__cell">', '</td>', $tr);
				$schedule['items'][] = array('day' => $day, 'time' => $time, 'rest' => $rest);
			}
			$remark = Model_SupplierParser_Abstract::_cut_str('<p class="b-schedule__remark">', '/p>', $block);
			$remark = Model_SupplierParser_Abstract::_cut_str('Время указано для региона:', '<', $remark);
			$schedule['remark'] = $remark;
			
			//echo '<pre>';
			//print_r($schedule);	
			
			// Способы оплаты
			$payment_methods = array();
			$block = Model_SupplierParser_Abstract::_cut_str('<div class="h-layout-hidden h-mb-15 qa-payment-methods-block">', '<div class="h-m-inline-block h-vertical-top h-m-width-230">', $html);
			while ($span = Model_SupplierParser_Abstract::_cut_str(' h-color-grey"', '/span>', $block)) {
				$span = Model_SupplierParser_Abstract::_cut_str('>', '<', $span);
				$payment_methods[] = $span;
			}
			
			//echo '<pre>';
			//print_r($payment_methods);	
			
			// Способы доставки
			$shipping_methods = array();
			$block = Model_SupplierParser_Abstract::_cut_str('<div class="h-layout-hidden h-mb-15 qa-delivery-methods-block">', '<div class="h-m-inline-block h-vertical-top h-m-width-230', $html);
			while ($span = Model_SupplierParser_Abstract::_cut_str(' h-color-grey"', '/span>', $block)) {
				$span = Model_SupplierParser_Abstract::_cut_str('>', '<', $span);
				$shipping_methods[] = $span;
			}
			
			//echo '<pre>';
			//print_r($shipping_methods);
			
			// Регионы доставки
			$delivery_regions = null;
			$delivery_regions_url = Model_SupplierParser_Abstract::_cut_str('data-deliveryinfo-url="', '"', $html);
			if ($delivery_regions_url) {
				$delivery_regions_url = 'http://tiu.ru'.$delivery_regions_url;
				$delivery_regions = Model_SupplierParser_Abstract::_get_source_code($delivery_regions_url);
				if (strstr($delivery_regions, 'Защита от роботов') !== false) {
					die('ban');
				}
				$delivery_regions = str_replace(' class="disc-marked"', '', $delivery_regions);
				$delivery_regions = str_replace(' class="grey"', '', $delivery_regions);
				$delivery_regions = str_replace(' class="medium-text h-mt-15"', '', $delivery_regions);
			}
			
			//echo $delivery_regions;
			
			// Контакты
			// http://votonia.tiu.ru/contacts
			$contacts = array(
				'name'    => null,
				'address' => null,
				'phone'   => null,
				'website' => null,
				'email'   => null,
				'skype'   => null,
				'other'   => array(),
			);
			$contacts_html = Model_SupplierParser_Abstract::_get_source_code($site_url);
			$dump_contacts_html = $contacts_html;
			$contacts['name']    = Model_SupplierParser_Abstract::_cut_str('<span class="b-contact-info__row"itemprop="name"title="Название">', '</span>', $contacts_html);
			
			$contacts['address'] = Model_SupplierParser_Abstract::_cut_str('<address ', '</address>', $contacts_html);
			if ($contacts['address']) {
				$contacts['address'] = str_replace('</span>', ', ', $contacts['address']);
				$contacts['address'] = strip_tags('<address '.$contacts['address']);
				$contacts['address'] = substr($contacts['address'], 0, strlen($contacts['address'])-2);
			}
			
			$phones_html = Model_SupplierParser_Abstract::_cut_str('class="b-contact-info__row"title="Телефон">', 'iv></div>', $contacts_html);
			if ($phones_html) {
				$contacts['phone'] = array();
				while ($div = Model_SupplierParser_Abstract::_cut_str('<div class="b-contact-info__phone', '</d', $phones_html)) {
					$phone = '<div class="b-contact-info__phone'.$div;
					$phone = str_replace('</span>', ' ', $phone);
					$phone = strip_tags($phone);
					$phone = str_replace('показать номер', '', $phone);
					$contacts['phone'][] = $phone;
				}
			}
			
			$contacts['website'] = Model_SupplierParser_Abstract::_cut_str('<div class="b-contact-info__row"title="Сайт"><a href="', '"', $contacts_html);
			$contacts['email']   = Model_SupplierParser_Abstract::_cut_str('<div class="b-contact-info__row"title="Email"><a href="mailto:', '"', $contacts_html);
			$contacts['skype']   = Model_SupplierParser_Abstract::_cut_str('<span class="b-contact-info__row"title="Skype">', '</span>', $contacts_html);
			
			while ($span = Model_SupplierParser_Abstract::_cut_str('<span class="b-contact-info__row"', '/span>', $contacts_html)) {
				$name  = Model_SupplierParser_Abstract::_cut_str('title="', '"', $span);
				$value = Model_SupplierParser_Abstract::_cut_str('>', '<', $span);
				$contacts['other'][$name] = $value;
			}
			
			//echo '<pre>';
			//print_r($contacts);
			
			if (count($schedule['items'])) {
				$schedule = json_encode($schedule);
			} else {
				$schedule = null;
			}
			if (count($payment_methods)) {
				$payment_methods = json_encode($payment_methods);
			} else {
				$payment_methods = null;
			}
			if (count($shipping_methods)) {
				$shipping_methods = json_encode($shipping_methods);
			} else {
				$shipping_methods = null;
			}
			if (is_array($contacts['phone']) && count($contacts['phone'])) {
				$contacts['phone'] = json_encode($contacts['phone']);
			} else {
				$contacts['phone'] = null;
			}
			if (count($contacts['other'])) {
				$contacts['other'] = json_encode($contacts['other']);
			} else {
				$contacts['other'] = null;
			}
			
			// сохраняем урл контактов и хтмл контактов   $supplier[0]
			$supplier = DB::insert('suppliers2', array('name',           'phone',             'address',            'worktime', 'website',            'email',            'skype',            'payment_options', 'delivery_methods', 'delivery_regions', 'contacts',         'url'))
						                ->values(array($contacts['name'], $contacts['phone'], $contacts['address'], $schedule,  $contacts['website'], $contacts['email'], $contacts['skype'], $payment_methods,  $shipping_methods,  $delivery_regions,  $contacts['other'], $site_url))->execute();
			DB::insert('suppliers2_html', array('id', 'html'))
					->values(array($supplier[0], $dump_contacts_html))->execute();
			
			// обновляем goods2 - supplier_id
			DB::update('goods2')
                    ->set(array('supplier_id' => $supplier[0]))
                    ->where('original_supplier_id', '=', $original_supplier_id)
                    ->execute();
			
		}
	}
	
	public function action_test44() 
    {
		set_time_limit(0);
		
		die();
	
		$categories = DB::select()
			->from('suppliers2')
			->where('name', 'IS', NULL)
			->where('url', '!=', 'contacts')
			->offset(0)
			->limit(5000)
			->execute()
			->as_array(); 
		echo count($categories);
		die();
		
		foreach ($categories as $category) {
			echo $category['id'].'<br />';
			
			// Контакты
			$contacts = array(
				'name'    => null,
				'address' => null,
				'phone'   => null,
				'website' => null,
				'email'   => null,
				'skype'   => null,
				'other'   => array(),
			);
			
			$contacts_html = DB::select()
				->from('suppliers2_html')
				->where('id', '=', $category['id'])
				->execute()
				->as_array();
			if (!isset($contacts_html[0]['html'])) {
				continue;
			}
			$contacts_html = $contacts_html[0]['html'];
			/*$contacts_html = Model_SupplierParser_Abstract::_get_source_code($category['url']);
			if (strstr($contacts_html, 'Защита от роботов') !== false) {
				die('ban');
			}*/
			
			$all = array();
			while($tr = Model_SupplierParser_Abstract::_cut_str('<tr class="b-contact-info__row">', '</tr>', $contacts_html)) {
				$name  = Model_SupplierParser_Abstract::_cut_str('<td class="b-contact-info__cell b-contact-info__cell_width_200">', '</td>', $tr);
				$name  = str_replace(':', '', $name);
				$value = Model_SupplierParser_Abstract::_cut_str('<td class="b-contact-info__cell"', 'd>', $tr);
				$value = Model_SupplierParser_Abstract::_cut_str('>', '</t', $value);
				$all[$name] = $value;
			}
			
			if (isset($all['Название'])) {
				$contacts['name']    = $all['Название'];
				unset($all['Название']);
			}
			if (isset($all['Адрес'])) {
				$contacts['address'] = $all['Адрес'];
				unset($all['Адрес']);
			}
			
			$contacts['phone'] = array();
			if (isset($all['Телефон'])) {
				while ($span = Model_SupplierParser_Abstract::_cut_str('<span>', '</span>', $all['Телефон'])) {
					$contacts['phone'][] = $span;
				}
				unset($all['Телефон']);
			}
			
			if (isset($all['Сайт'])) {
				$contacts['website'] = $all['Сайт'];
				unset($all['Сайт']);
			}
			if (isset($all['Email'])) {
				$contacts['email']   = $all['Email'];
				unset($all['Email']);
			}
			if (isset($all['Skype'])) {
				$contacts['skype']   = $all['Skype'];
				unset($all['Skype']);
			}
			
			$contacts['other'] = $all;
			
			if (is_array($contacts['phone']) && count($contacts['phone'])) {
				$contacts['phone'] = json_encode($contacts['phone']);
			} else {
				$contacts['phone'] = null;
			}
			if (count($contacts['other'])) {
				$contacts['other'] = json_encode($contacts['other']);
			} else {
				$contacts['other'] = null;
			}
			
			// сохраняем урл контактов и хтмл контактов   $supplier[0]
			$supplier = DB::update('suppliers2')
								->set(array('name' => $contacts['name'], 'phone' => $contacts['phone'], 'address' => $contacts['address'], 'website' => $contacts['website'], 'email' => $contacts['email'], 'skype' => $contacts['skype'], 'contacts' => $contacts['other']))
						        ->where('id', '=', $category['id'])
								->execute();
		}
	}
	
	public function action_test45() 
    {
		set_time_limit(0);
		die();
			
		$regions = DB::select()
			->from('regions')
			->order_by('parent_id', 'DESC')
			->execute()
			->as_array();
			
		$suppliers = DB::select()
			->from('suppliers2')
			//->offset(100)
			//->limit(50)
			->execute()
			->as_array(); 
			
		foreach ($suppliers as $supplier) {
			echo $supplier['id'].' - '.$supplier['address'];
			$region_id = 0;
			foreach ($regions as $region) {
				if (strstr($supplier['address'], $region['name']) !== false) {
					$region_id = $region['id'];
					echo ' - <strong>'.$region['name'].'</strong>';
					break;
				}
			}
			if ($region_id) {
				$supplier = DB::update('suppliers2')
								->set(array('region_id' => $region_id))
						        ->where('id', '=', $supplier['id'])
								->execute();
			}
			echo '<br />';
		}
		
	}
	
	public function action_test46() 
    {
		set_time_limit(0);
		die();
			
		$regions = DB::select()
			->from('regions')
			->order_by('parent_id', 'DESC')
			->execute()
			->as_array();
			
		$suppliers = DB::select()
			->from('suppliers')
			//->offset(0)
			//->limit(50)
			->execute()
			->as_array(); 
			
		foreach ($suppliers as $supplier) {
			echo $supplier['id'].' - '.$supplier['address'];
			$region_id = 0;
			foreach ($regions as $region) {
				if (strstr($supplier['address'], $region['name']) !== false) {
					$region_id = $region['id'];
					echo ' - <strong>'.$region['name'].'</strong>';
					break;
				}
			}
			if ($region_id) {
				$supplier = DB::update('suppliers')
								->set(array('region_id' => $region_id))
						        ->where('id', '=', $supplier['id'])
								->execute();
			}
			echo '<br />';
		}
		
	}
	
	public function action_test47() 
    {
		set_time_limit(0);
		die();
		
		$suppliers = DB::select()
			->from('suppliers')
			//->offset(0)
			//->limit(1)
			->execute()
			->as_array();
			
		foreach ($suppliers as $supplier) {
			//echo $supplier['id'];
			if ($supplier['region_id']) {
				DB::update('goods')
					->set(array('region_id' => $supplier['region_id']))
					->where('supplier_id', '=', $supplier['id'])
					->execute();
			}
		}
	}
	
	public function action_test48() 
    {
		set_time_limit(0);
		die();
		
		$suppliers = DB::select()
			->from('suppliers1')
			//->offset(0)
			//->limit(1)
			->execute()
			->as_array();
			
		foreach ($suppliers as $supplier) {
			DB::query(Database::INSERT, "
				INSERT INTO suppliers (
					id, source, region_id, name, type, volume, min_order, payment_options, delivery_methods, legal_person, payment_details, description, buy_opt, nalog_system,
					phone, mobile, address, worktime, website, email, skype, url, is_active_phone, is_active_email, is_paid, is_active
				) VALUES (
					'{$supplier['id']}', 'optlist', '{$supplier['region_id']}', '".str_replace("'", "\'", $supplier['name'])."', '{$supplier['type']}', '{$supplier['volume']}', '{$supplier['min_order']}', '{$supplier['payment_options']}', '{$supplier['delivery_methods']}', '".str_replace("'", "\'", $supplier['legal_person'])."', '{$supplier['payment_details']}', '".str_replace("'", "\'", $supplier['description'])."', '".str_replace("'", "\'", $supplier['buy_opt'])."', '{$supplier['nalog_system']}',
					'{$supplier['phone']}', '{$supplier['mobile']}', '".str_replace("'", "\'", $supplier['address'])."', '{$supplier['worktime']}', '{$supplier['website']}', '{$supplier['email']}', '{$supplier['skype']}', '{$supplier['url']}', '{$supplier['active_phone']}', '{$supplier['active_email']}', '{$supplier['is_paid']}', '{$supplier['is_active']}'
				);
			")->execute();
		}
	}
	
	public function action_test49() 
    {
		set_time_limit(0);
		
		$suppliers = DB::select()
			->from('suppliers2')
			//->offset(0)
			//->limit(1)
			->execute()
			->as_array();
			
		foreach ($suppliers as $supplier) {
			DB::query(Database::INSERT, "
				INSERT INTO suppliers (
					id, source, region_id, name, phone, address, worktime, website, email, skype, payment_options, delivery_methods, delivery_regions, contacts, url
				) VALUES (
					'{$supplier['id']}', 'tiu', '{$supplier['region_id']}', '{$supplier['name']}', '{$supplier['phone']}', '{$supplier['address']}', '{$supplier['worktime']}', '{$supplier['website']}', '{$supplier['email']}', '{$supplier['skype']}', '{$supplier['payment_options']}', '{$supplier['delivery_methods']}', '".str_replace("'", "\'", $supplier['delivery_regions'])."', '".str_replace("'", "\'", $supplier['contacts'])."', '{$supplier['url']}'
				);
			")->execute();
		}
	}
	
	public function action_test50() 
    {
		set_time_limit(0);
		
		$supplier = DB::select()
			->from('suppliers2')
			->where('id', '=', 23969)
			->execute()
			->as_array();
		$supplier = $supplier[0];
		
		//print_r(json_decode($supplier['contacts'], true));
		
		/*$suppliers = DB::select()
			->from('suppliers2')
			->where('website', 'LIKE', DB::expr("'%href%'"))
			->execute()
			->as_array();
			
		foreach ($suppliers as $supplier) {
			$website = Model_SupplierParser_Abstract::_cut_str('<a href="', '"', $supplier['website']);
			//echo $supplier['id'].' - '.$website.'<br />';
			DB::update('suppliers2')
					->set(array('website' => $website))
					->where('id', '=', $supplier['id'])
					->execute();
		}*/
	}
	
	// сначала перегоняем товары из тиу
	//  перегнать price у оптлиста
	public function action_test51() 
    {
		set_time_limit(0);
		die();
		
		$goods = DB::select()
			->from('goods2')
			->where('id', '>', 2734692)
			->offset(0)
			->limit(100000)
			->execute()
			->as_array();
			
		foreach ($goods as $good) {
			DB::query(Database::INSERT, "
				INSERT INTO goods (
					id, source, source_id, supplier_id, region_id, img, name, price, currency, description, prices, url
				) VALUES (
					'{$good['id']}', 'tiu', '{$good['original_id']}', '{$good['supplier_id']}', '{$good['region_id']}', '{$good['img']}', '".str_replace("'", "\'", str_replace("\\", '', $good['name']))."', '{$good['price']}', '{$good['currency']}', '".str_replace("'", "\'", str_replace("\\", '', $good['description']))."', '{$good['prices']}', '{$good['url']}'
				);
			")->execute();
		}
	}
	
	public function action_test52() 
    {
		set_time_limit(0);
		die();
		
		$goods = DB::select()
			->from('goods1')
			->where('id', '>', 145292)
			->offset(200000)
			->limit(100000)
			->execute()
			->as_array();
		
		//<b>5 - 6 руб.</b> (мин. заказ: 330 шт.)
		//<b>612 руб.</b>
		//<b>13000 руб.</b> (мин. заказ: 10 шт.)
		
		//{"price":12300,"count":"2 \u0448\u0442."}
		foreach ($goods as $good) {
			if (strstr($good['img'], 'http') === false) {
				$good['img'] = 'http://optlist.ru'.$good['img'];
			}
			$good['url'] = 'http://optlist.ru'.$good['url'];
			
			$original_price = $good['price'];
			$prices = null;
			$price  = $_price = str_replace(' руб.', '', Model_SupplierParser_Abstract::_cut_str('<b>', '</b>', $good['price']));
			if (strstr($price, ' - ') !== false) {
				list($min_price, $max_price) = explode(' - ', $price);
				$price = $min_price;
			}
			if ($count = Model_SupplierParser_Abstract::_cut_str('(мин. заказ: ', ')', $good['price'])) {
				$count = str_replace(' шт.', '', $count);
			}
			if ($_price != $price || $count) {
				$prices = array(
					'price' => $_price,
					'count' => $count,
				);
				$prices = json_encode($prices);
			}
			
			/*echo $original_price.'<br />';
			echo $price.'<br />';
			print_r($prices);
			echo '<hr />';*/
			
			
			
		
			DB::query(Database::INSERT, "
				INSERT INTO goods (
					source, source_id, supplier_id, region_id, img, name, price, description, prices, url
				) VALUES (
					'optlist', '0', '{$good['supplier_id']}', '{$good['region_id']}', '{$good['img']}', '".str_replace("'", "\'", str_replace("\\", '', $good['name']))."', '{$price}', '".str_replace("'", "\'", str_replace("\\", '', $good['description']))."', '{$prices}', '{$good['url']}'
				);
			")->execute();
		}
	}
	
	public function action_test53() 
    {
		set_time_limit(0);
		die();
		
		$goods = DB::select('id', 'original_id', 'category_id')
			->from('goods2')
			->where('id', '>', 794533)
			->order_by('id', 'ASC')
			->offset(0)
			->limit(400000)
			->execute()
			->as_array();
		
		foreach ($goods as $good) {
			$cat_ids = array();
			$cat_ids[$good['category_id']] = $good['category_id'];
			
			$cats = DB::select('category_id')
						->from('goods2_categories2')
						->where('good_original_id', '=', $good['original_id'])
						->execute()
						->as_array();
						
			foreach ($cats as $cat) {
				$cat_ids[$cat['category_id']] = $cat['category_id'];
			}
			
			foreach ($cat_ids as $cat_id) {
				DB::query(Database::INSERT, "
					INSERT INTO good_categories (
						good_id, category_id
					) VALUES (
						'{$good['id']}', '{$cat_id}'
					);
				")->execute();
			}
		}
		
	}
	
	public function action_test54() 
    {
		set_time_limit(0);
		die();
		
		$suppliers = DB::select()
			->from('suppliers')
			->where('source', '=', 'tiu')
			->where('id', '>', 58168)
			->order_by('id', 'ASC')
			->offset(0)
			->limit(10000)
			->execute()
			->as_array();
		
		foreach ($suppliers as $supplier) {
			$ids = DB::query(Database::SELECT, 'SELECT gc.category_id FROM goods AS g
											    JOIN good_categories AS gc ON (gc.good_id = g.id)
												WHERE g.supplier_id = '.$supplier['id'].'
												GROUP BY gc.category_id'
						)->execute()->as_array();
									
			foreach ($ids as $id) {
				$id = $id['category_id'];
				DB::query(Database::INSERT, "
					INSERT INTO supplier_categories (
						supplier_id, category_id
					) VALUES (
						'{$supplier['id']}', '{$id}'
					);
				")->execute();
			}
		}
		
	}
    
    public function action_test55() 
    {
        set_time_limit(0);
        die();
        
        function get_childs($category, &$result) {
            $childs = DB::select()
                ->from('categories')
                ->where('parent_id', '=', $category['id'])
                ->execute()
                ->as_array();
            foreach ($childs as $child) {
                $result[] = $child['id']; 
                get_childs($child, $result);   
            }   
        }
        
        $result = array();
        $categories = DB::select()
                ->from('categories')
                ->execute()
                ->as_array();
        $result = array();
        foreach ($categories as $cat) {
            $result[$cat['id']] = array();
            get_childs($cat, $result[$cat['id']]);
        }
        
        foreach ($categories as $cat) {
            if (count($result[$cat['id']])) {
                DB::update('categories')
                        ->value('childs', implode(',', $result[$cat['id']]))
                        ->where('id', '=', $cat['id'])
                        ->execute();
            }
        }
    }
    
    public function action_test56() 
    {
        set_time_limit(0);
        die();
        
        $categories = DB::select()
                ->from('categories')
                ->execute()
                ->as_array();
        foreach ($categories as $category) {
            $category_url = Model_Main::translit($category['name']);
            if ($category['pointer_id']) {
                $category_url .= '-double';   
            }
            
            DB::update('categories')
                ->set(array('url' => $category_url))
                ->where('id', '=', $category['id'])
                ->execute();    
        } 
        
        /*UPDATE `categories` SET count_suppliers = (
            SELECT COUNT(*)
            FROM suppliers AS s
            JOIN supplier_categories AS sc ON (sc.supplier_id = s.id)
            WHERE sc.category_id = categories.id
        )  */
    }
    
    public function action_test57() 
    {
        set_time_limit(0);
        
        $categories = DB::select()
                ->from('categories')
                ->order_by('id', 'ASC')
                ->execute()
                ->as_array();
        foreach ($categories as $category) {
            $ids = array();
            if ($category['childs']) {
                $ids = explode(',', $category['childs']);  
            } 
            $ids[] = $category['id'];  
            
            DB::query(Database::UPDATE, "
                    UPDATE categories SET count_suppliers = (
                        SELECT COUNT(*)
                        FROM suppliers AS s
                        JOIN supplier_categories AS sc ON (sc.supplier_id = s.id)
                        WHERE sc.category_id IN (".implode(',', $ids).")
                    ) WHERE id = {$category['id']}
                ")->execute();
        }
    }
    
    public function action_test58() 
    {
        set_time_limit(0);
        
        $categories = DB::select()
                ->from('categories')
                ->order_by('id', 'ASC')
                ->execute()
                ->as_array();
        foreach ($categories as $category) {
            $ids = array();
            if ($category['childs']) {
                $ids = explode(',', $category['childs']);  
            } 
            $ids[] = $category['id'];  
            
            DB::query(Database::UPDATE, "
                    UPDATE categories SET count_goods = (
                        SELECT COUNT(*)
                        FROM goods AS g
                        JOIN good_categories AS gc ON (gc.good_id = g.id)
                        WHERE gc.category_id IN (".implode(',', $ids).")
                    ) WHERE id = {$category['id']}
                ")->execute();
        }
    }
} 
